package logic

import (
	"context"
	"errors"
	"video-hub/models/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type VideoBacLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoBacLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoBacLogic {
	return &VideoBacLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoBacLogic) VideoBac(in *videorpc.VideoBacRequest) (*videorpc.VideoBacResponse, error) {
	// todo: add your logic here and delete this line
	var video mysql.Video
	video = mysql.Video{
		Model:       gorm.Model{ID: video.ID},
		VideoTitle:  in.VideoTitle,
		VideoLink:   in.VideoLink,
		VideoStatus: in.VideoStatus,
		TopicId:     in.TopicId,
	}
	if in.VideoStatus == "1" {
		if err := video.Updatavideo(mysql.DB, in.VideoId); err != nil {
			return nil, err
		} else if err = video.DelVideo(mysql.DB, in.VideoId); err != nil {
			return nil, errors.New("确定删除该草稿？删除后不可恢复")
		}
	} else if in.VideoStatus == "0" || in.VideoStatus == "2" {
		return nil, errors.New("操作成功")
	}
	return &videorpc.VideoBacResponse{
		VideoId: int64(video.ID),
	}, nil
}
